package manager

import (
	"NovelBook/logic/worker"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 离职

func SetWorkerOff(c *gin.Context) {
	idAny, exist := c.Get(auth.ManagerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	var req struct {
		IDs []uint `json:"id" binding:"required"`
	}

	if err := c.BindJSON(&req); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	err := worker.SetWorkersOff(req.IDs)

	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "system busy")
		return
	}

	response.Success(c, "successfully")
}

// 复职

func SetWorkerOn(c *gin.Context) {
	idAny, exist := c.Get(auth.ManagerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	var req struct {
		IDs []uint `json:"id" binding:"required"`
	}

	if err := c.BindJSON(&req); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	err := worker.SetWorkersOn(req.IDs)

	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "system busy")
		return
	}

	response.Success(c, "successfully")
}
